package com.hontech.libratest.activity.fragment.sub

import android.view.View
import android.widget.Button
import android.widget.EditText
import com.hontech.libratest.R
import com.hontech.libratest.activity.window.showProg
import com.hontech.libratest.conn.*
import com.hontech.libratest.utils.onClick
import com.hontech.libratest.utils.toInt

class ElevatorMotorSub(view: View) {

    private val btnReset = view.findViewById<Button>(R.id.id_main_btn_elevator_reset)
    private val btnRow = view.findViewById<Button>(R.id.id_main_btn_elevator_row)
    private val btnHigh = view.findViewById<Button>(R.id.id_main_btn_elevator_ms)

    private val etRow = view.findViewById<EditText>(R.id.id_main_et_elevator_row)
    private val etMs = view.findViewById<EditText>(R.id.id_main_et_elevator_ms)

    private val btnHighSet = view.findViewById<Button>(R.id.id_main_btn_high_set)
    private val etHighRow = view.findViewById<EditText>(R.id.id_main_et_high_row)
    private val etHighMs = view.findViewById<EditText>(R.id.id_main_et_high_ms)
    private val btnHighGet = view.findViewById<Button>(R.id.id_main_btn_high_get)

    private val btnEnable = view.findViewById<Button>(R.id.id_main_btn_elevator_enable)
    private val btnDisable = view.findViewById<Button>(R.id.id_main_btn_elevator_disable)
    private val btnGetEnable = view.findViewById<Button>(R.id.id_main_btn_elevator_get_enable)

    private val btnGetSpeed = view.findViewById<Button>(R.id.id_main_btn_elevator_get_speed)
    private val btnSetSpeed = view.findViewById<Button>(R.id.id_main_btn_elevator_set_speed)

    private val etBaseFreq = view.findViewById<EditText>(R.id.id_main_et_base_freq)
    private val etVarRange = view.findViewById<EditText>(R.id.id_main_et_var_range)
    private val etAcc = view.findViewById<EditText>(R.id.id_main_et_acc)

    init {
        btnReset.onClick(::onClickReset)
        btnRow.onClick(::onClickRow)
        btnHigh.onClick(::onClickHigh)
        btnHighSet.onClick(::onClickHighSet)
        btnHighGet.onClick(::onClickHighGet)
        btnEnable.onClick(::onClickSetEnable)
        btnDisable.onClick(::onClickSetDisable)
        btnGetEnable.onClick(::onClickGetEnable)
        btnGetSpeed.onClick(::onClickGetSpeed)
        btnSetSpeed.onClick(::onClickSetSpeed)
    }

    private suspend fun onClickReset() {
        val w = showProg(btnReset, "升降电机复位")
        try {
            testElevatorByRow(0)
            w.success("升降电机复位成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickRow() {
        val row = etRow.toInt()
        val w = showProg(btnReset, "移动到第${row}行")
        try {
            testElevatorByRow(1, row)
            w.success("移动到第${row}行成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickHigh() {
        val ms = etMs.toInt()
        val w = showProg(btnReset, "移动到${ms}")
        try {
            testElevatorByMs(ms)
            w.success("移动到:${ms}成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickHighSet() {
        val row = etHighRow.toInt()
        val ms = etHighMs.toInt()
        val w = showProg(btnReset, "设置$row => $ms")
        try {
            setElevatorRowHigh(row, ms)
            w.success("设置$row => $ms 成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickHighGet() {
        val row = etHighRow.toInt()
        val w = showProg(btnReset, "查询层高")
        try {
            val high = getElevatorRowHigh(row)
            w.success("第${row}高度:${high}mm")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickSetEnable() {
        val w = showProg(btnReset, "启用升降")
        try {
            enableElevator()
            w.success("启用升降成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickSetDisable() {
        val w = showProg(btnReset, "禁用升降")
        try {
            disableElevator()
            w.success("禁用升降成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickGetEnable() {
        val w = showProg(btnReset, "查询升降是否启动?")
        try {
            val ret = getUseElevator()
            if (ret) {
                w.success("升降启用")
            } else {
                w.success("升降禁用")
            }
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickGetSpeed() {
        val w = showProg(btnReset, "查询升降速度")
        try {
            val sp = getElevatorSpeed()
            w.success("BaseFreq=${sp.baseFreq} VarRange=${sp.varRange} Acc=${sp.acc}")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickSetSpeed() {
        val baseFreq = etBaseFreq.toInt()
        val varRange = etVarRange.toInt()
        val acc = etAcc.toInt()
        val w = showProg(btnReset, "设置升降速度")
        try {
            setElevatorSpeed(baseFreq, varRange, acc)
            w.success("设置升降速度成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

}